package com.jinkeen.xiaxie.service

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import android.os.SystemClock
import android.util.Log
import androidx.core.app.NotificationCompat
import com.jinkeen.xiaxie.MainActivity
import com.jinkeen.xiaxie.R
import com.jinkeen.xiaxie.anim.ColorEvalutor

/**
 * @description: 前台服务
 * @author whz
 * @date 2021/6/3 15:56
 * @version 1.0
 */

class WhiteService : Service() {
    val TAG = "WhiteService"
    override fun onCreate() {
        super.onCreate()
        Log.e(TAG, "onCreate================")
        var notification=createNotification()
        startForeground(110,notification)
    }

    override fun onBind(intent: Intent?): IBinder? {
        Log.e(TAG, "onBind==================")
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.e(TAG, "onStartCommand====================")
        //开启一条线程 去移除通知
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
            var build=Notification.Builder(this)
            build.setSmallIcon(R.mipmap.ic_launcher)
            startForeground(110,build.build())
            //开启一条线程 去移除通知
            Thread { // 延迟1s
                SystemClock.sleep(1000)
                // 取消CancelNoticeService的前台
                stopForeground(true)
                // 移除DaemonService弹出的通知
                val manager =
                    getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                manager.cancel(110)
                // 任务完成，终止自己
                stopSelf()
            }.start()
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        Log.e(TAG, "onDestory=====================")
        super.onDestroy()
    }


    private fun createNotification(): Notification {
        var notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        var notificationChanneID = "notification_whiteService"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            var channeName = "WhiteService Notification"
            var importance = NotificationManager.IMPORTANCE_HIGH
            var notificationCHannel =
                NotificationChannel(notificationChanneID, channeName, importance)
            notificationCHannel.description = "123"
            notificationCHannel.enableLights(true)
            notificationCHannel.lightColor = Color.RED
//            notificationCHannel.vibrationPattern=Long[]{10,100}
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationCHannel)
            }
        }
        var builder=NotificationCompat.Builder(this,notificationChanneID)
//        builder.setSmallIcon(R.mipmap.touxiang_man)
//        builder.setContentTitle("title")
//        builder.setContentText("text")
        builder.setWhen(System.currentTimeMillis())
        var activityInent=Intent(this,MainActivity::class.java)
        var pendingIntent=PendingIntent.getActivity(this,1,activityInent,PendingIntent.FLAG_CANCEL_CURRENT)
        builder.setContentIntent(pendingIntent)
        return builder.build()
    }
}